package com.music.controller;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Controller;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.web.mvc.annotation.RequestMapping;
import com.laoxuai.web.mvc.annotation.ResponseBody;
import com.music.pojo.Tag;
import com.music.service.TagService;
import com.music.util.Result;

import java.util.List;

@Controller
@RequestMapping("/tag")
public class TagController {

    @Autowired
    private TagService tagServiceImpl;

    /***
     * 新增
     * @param tag
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(Tag tag) {
        int result = tagServiceImpl.insert(tag);
        if(result == 0){
            return Result.failed("添加失败",result);
        }
        return Result.success("添加成功", result);
    }

    /***
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(Integer id) {
        int result = tagServiceImpl.delete(id);
        return Result.success("删除成功", result);
    }

    /***
     * 修改
     * @param tag
     * @param id
     * @return
     */
    @RequestMapping(value="/update")
    @ResponseBody
    public Result update(Integer id, Tag tag) {
        tag.setId(id);
        int result = tagServiceImpl.update(tag);
        return Result.success("修改成功");
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/selectAll")
    @ResponseBody
    public Result selectAll(){
        List<Tag> list = tagServiceImpl.selectAll();
        if (list == null){
            return Result.failed("查询失败");
        }
        return Result.success("查询成功",list);
    }

    /***
     * 主键查询
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public Result getByPrimaryKey(Integer id) {
        Tag tag = tagServiceImpl.getByPrimaryKey(id);
        return Result.success("查询成功", tag);
    }

    /**
     * 分页查询
     * @param tag
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/page")
    @ResponseBody
    public Result page(Tag tag, int page, int size) {
        PageInfo<Tag> pageInfo = tagServiceImpl.listPageInfo(tag, page, size);
        return Result.success("查询成功", pageInfo);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(Tag tag, int page, int size) {
        List<Tag> list = tagServiceImpl.listPage(tag, page, size);
        return Result.success("查询成功", list);
    }
}